/**
 * 用户管理
 */

$(function(){
//	$('#companyDeptTree').datagrid({
//		url				: '/pc/systemUser/allCompany.htm',
//		fit				: true,
//		fitColumns		: true,
//		striped			: true,
//		rownumbers		: true,
//		autoRowHeight	: true,
//		singleSelect    : true,
//		nowrap			: true,
//		pagination		: true,
//		pageSize        : 20,
//		pageList        : [10, 20, 30, 40, 50],
//		columns			: [[
//		    {field : 'companyName' , title:'公司' , width : 100,   hidden : true},
//		]],
//		onClickRow      : function(rowIndex, rowData) {
//        	var industryId = rowData.industryId;
//        	$('#systemUserGrid').datagrid({ 
//        		url         : '/pc/property/list.htm', 
//                queryParams : {	industryId : industryId }
//        	}).load();
//        },
//	});
//	
	 //公司部门树
	$('#companyDeptTree').tree({
		url:'/pc/department/tree.htm',
		idField : 'id',
		treeField : 'text',
		onClick: function(node){
			var departmentid = node.id;
			$('#systemUserGrid').datagrid({
				url:'/pc/systemUser/all.htm', 
				queryParams:{
					departmentid:departmentid
				}
			}).load();
			
		}
	});
	
	//系统用户列表
    $('#systemUserGrid').datagrid({  
        fitColumns:true,
        striped:true,
        fit:true,
        rownumbers:true,
        singleSelect:false,
        autoRowHeight:true,
        nowrap:true,
        pagination:true,
        pageSize:20,
        loadMsg:"载入中...",
        columns:[[ 
            {field:'systemUser.rolebox',title : 'systemUser.rolebox',checkbox : true},      
            {field:'userId',title:'id',hidden:true,width:100},  
            {field:'userName',title:'用户名',width:100},  
            {field:'realName',title:'用户姓名',width:100},
            {field:'companyName',title:'公司名称',width:100},
            {field:'email',title:'邮箱',width:100},
            {field:'mobile',title:'手机号码',width:100},
            {field:'loginTime',title:'上次登录时间',width:100},
            {field:'loginCount',title:'登录次数',width:100},
        ]],
        toolbar: [{
    		iconCls: 'add',
    		text:'添加',
    		handler: function(){
			  Ict.openWin("用户添加","350","250","/pc/systemUser/add.htm");
    		}
    	},{
			text : '修改',
			iconCls : 'edit',
			handler : function() {
				var row = $('#systemUserGrid').datagrid('getSelections');
				if (row.length == 0) {
					Ict.error('请选择你要修改的用户',
							function() {
								return;
							});
				} else if (row.length > 1) {
					Ict.error('请选择一条记录进行修改',
							function() {
								return;
							});
				} else if (row.length == 1)
					Ict.openWin('用户修改',350,250,'/pc/systemUser/edit.htm?systemUserId='
											+ row[0].userId);
			}
		},{
			text : '删除',
			iconCls : 'remove',
			handler : function() {
			  var rows = $('#systemUserGrid').datagrid('getSelections');
			  var num = rows.length;
			  var systemUserId = null;
			  if (rows.length == 0) {
				Ict.error('请选择你要删除的用户',function() {
						return;
				});
			  } else {
				Ict.confirm("确定要删除所选记录？",function(result) {
				  if (result) {
					for ( var i = 0; i < num; i++) {
					  if (null == systemUserId|| i == 0) {
						  systemUserId = rows[i].userId;
					  } else {
						  systemUserId = systemUserId+ "," + rows[i].userId;
					  }
				   }
				   $.ajax({url : "/pc/systemUser/delSystemUser.htm",
						  type : 'get',
						  context : document.body,
						  data : {"systemUserId" : systemUserId},
						  dataType : 'json',
						  success : function(data) {
							 if (data == 1) {
							    Ict.info("删除成功!",function() {
									$("#systemUserGrid").datagrid("reload");});
						     } else {
						    	 Ict.error("删除失败！稍后重试。",function() {
									return;
								 });
							 }
						   }
					 });
					}
				  });
				}
			}
		},]
    });  

   
});